package cn.iocoder.yudao.module.member.controller.admin.shunfengcheorder;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.shunfengcheorder.vo.*;
import cn.iocoder.yudao.module.member.convert.shunfengcheorder.ShunfengcheOrderConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.shunfengcheorder.ShunfengcheOrderDO;
import cn.iocoder.yudao.module.member.service.shunfengcheorder.ShunfengcheOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 公众号顺风车订单")
@RestController
@RequestMapping("/member/mp/shunfengche-order")
@Validated
public class ShunfengcheOrderController {

    @Resource
    private ShunfengcheOrderService shunfengcheOrderService;

    @PostMapping("/create")
    @ApiOperation("创建公众号顺风车订单")
    @PreAuthorize("@ss.hasPermission('mp:shunfengche-order:create')")
    public CommonResult<Long> createShunfengcheOrder(@Valid @RequestBody ShunfengcheOrderCreateReqVO createReqVO) {
        return success(shunfengcheOrderService.createShunfengcheOrder(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新公众号顺风车订单")
    @PreAuthorize("@ss.hasPermission('mp:shunfengche-order:update')")
    public CommonResult<Boolean> updateShunfengcheOrder(@Valid @RequestBody ShunfengcheOrderUpdateReqVO updateReqVO) {
        shunfengcheOrderService.updateShunfengcheOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除公众号顺风车订单")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('mp:shunfengche-order:delete')")
    public CommonResult<Boolean> deleteShunfengcheOrder(@RequestParam("id") Long id) {
        shunfengcheOrderService.deleteShunfengcheOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得公众号顺风车订单")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('mp:shunfengche-order:query')")
    public CommonResult<ShunfengcheOrderRespVO> getShunfengcheOrder(@RequestParam("id") Long id) {
        ShunfengcheOrderDO shunfengcheOrder = shunfengcheOrderService.getShunfengcheOrder(id);
        return success(ShunfengcheOrderConvert.INSTANCE.convert(shunfengcheOrder));
    }


    @GetMapping("/cancel")
    @ApiOperation("取消公众号顺风车订单")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('mp:shunfengche-order:update')")
    public CommonResult<Boolean> cancel(@RequestParam("id") Long id) {
        return success(shunfengcheOrderService.changeOrderStatus(id, 5));
    }

    @GetMapping("/reSend")
    @ApiOperation("重派顺风车订单")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('mp:shunfengche-order:update')")
    public CommonResult<Boolean> reSend(@RequestParam("id") Long id) {

        return success(shunfengcheOrderService.reSend(id));
    }

    @GetMapping("/list")
    @ApiOperation("获得公众号顺风车订单列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('mp:shunfengche-order:query')")
    public CommonResult<List<ShunfengcheOrderRespVO>> getShunfengcheOrderList(@RequestParam("ids") Collection<Long> ids) {
        List<ShunfengcheOrderDO> list = shunfengcheOrderService.getShunfengcheOrderList(ids);
        return success(ShunfengcheOrderConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得公众号顺风车订单分页")
    @PreAuthorize("@ss.hasPermission('mp:shunfengche-order:query')")
    public CommonResult<PageResult<ShunfengcheOrderRespVO>> getShunfengcheOrderPage(@Valid ShunfengcheOrderPageReqVO pageVO) {
        PageResult<ShunfengcheOrderDO> pageResult = shunfengcheOrderService.getShunfengcheOrderPage(pageVO);
        return success(ShunfengcheOrderConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出公众号顺风车订单 Excel")
    @PreAuthorize("@ss.hasPermission('mp:shunfengche-order:export')")
    @OperateLog(type = EXPORT)
    public void exportShunfengcheOrderExcel(@Valid ShunfengcheOrderExportReqVO exportReqVO,
                                            HttpServletResponse response) throws IOException {
        List<ShunfengcheOrderDO> list = shunfengcheOrderService.getShunfengcheOrderList(exportReqVO);
        // 导出 Excel
        List<ShunfengcheOrderExcelVO> datas = ShunfengcheOrderConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "公众号顺风车订单.xls", "数据", ShunfengcheOrderExcelVO.class, datas);
    }

}
